package com.qs.workspace.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.qs.workspace.R;
import com.qs.workspace.sweep.MipcaActivityCapture;
import com.qs.workspace.sweep.encoding.EncodingHandler;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et_scan;
    private ImageView iv_bitmap;

    private static final int REQUEST_CODE=1;

    public static String SCAN_RESULT="scan_result";


    int a = 0;
    int b=0;
    int d=7;
    int e=77;
    int f=3333;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();


    }

    private void initView(){
        et_scan= (EditText) findViewById(R.id.et_scan);
        Button btn_scan= (Button) findViewById(R.id.btn_scan);
        Button btn_create_qr= (Button) findViewById(R.id.btn_create_qr);
        Button btn_create_q= (Button) findViewById(R.id.btn_create_q);
        btn_scan.setOnClickListener(this);
        btn_create_qr.setOnClickListener(this);
        btn_create_q.setOnClickListener(this);

        iv_bitmap= (ImageView) findViewById(R.id.iv_bitmap);



       /* btn_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, MipcaActivityCapture.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //intent.putExtra("numType", "icNum");
                startActivityForResult(intent,REQUEST_CODE);
            }
        });*/




    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==RESULT_OK){
            if (requestCode == 1) {

                String str=data.getStringExtra(SCAN_RESULT);
                if (str==null){
                    Toast.makeText(this,"请扫描正确的条码！",Toast.LENGTH_SHORT).show();
                    et_scan.setText("");
                }else {
                    et_scan.setText(str);
                    et_scan.setSelection(str.length());
                }
               /* if (str.length()>11) {
                    String res = str.substring(0, 3) + "-" + str.substring(3, 11);
                    etScan.setText(res);
                }else if(str.length()>3){
                    String res = str.substring(0, 3) + "-" + str.substring(3);
                    etScan.setText(res);
                }else {
                    Toast.makeText(this,"请扫描正确的条码！",Toast.LENGTH_SHORT).show();
                    etScan.setText("");
                }*/
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_scan:
                Intent intent=new Intent(MainActivity.this, MipcaActivityCapture.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //intent.putExtra("numType", "icNum");
                startActivityForResult(intent,REQUEST_CODE);
                break;
            case R.id.btn_create_qr:
                String edit = et_scan.getText().toString();
                if (!edit.equals("")) {
                    Bitmap qrCodeBitmap = EncodingHandler.createQRCode(edit, 350);
                    if(qrCodeBitmap!=null) iv_bitmap.setImageBitmap(qrCodeBitmap);
                    else Toast.makeText(this, "二维码生成失败！", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "Text can not be empty", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_create_q:
                String edit1 = et_scan.getText().toString();
                if (!edit1.equals("")) {

                    Bitmap qrCodeBitmap = EncodingHandler.creatBarcode(this, edit1, 350, 150, true);
                    if(qrCodeBitmap!=null) iv_bitmap.setImageBitmap(qrCodeBitmap);
                    else Toast.makeText(this, "条形码生成失败！", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
